package com.wankuma.hcm.midlet.metwit;

import METwit;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;

public class UpdateScreen extends TextBox implements CommandListener {

	private static UpdateScreen instance;
	
	private Display display;
	private Displayable mainScreen;

	private Command cmdSend = new Command("送信", Command.OK, 1);
	private Command cmdCancel = new Command("取消", Command.CANCEL, 2);
	private Command cmdEncodeText = new Command("ｴﾝｺｰﾄﾞ確認", Command.ITEM, 100);
	
	private UpdateScreen(){
		super("ｽﾃｰﾀｽ更新", null, 140, TextField.ANY);

		this.display = METwit.getDisplay();
		this.mainScreen = METwit.getMainScreen();

		this.addCommand(cmdSend);
		this.addCommand(cmdCancel);
		this.addCommand(cmdEncodeText);
		this.setCommandListener(this);	
	}
	
	public synchronized static UpdateScreen getInstance(String str){
		if(instance == null){
			instance = new UpdateScreen();
		}
		instance.setString(str);
		return instance;
	}
	
	public static UpdateScreen getInstance(){
		return getInstance(null);
	}

	public void commandAction(Command c, Displayable d) {
		if (c == cmdSend) {
			String str = this.getString();
			UpdateThread t = new UpdateThread(str);
			t.start();
			display.setCurrent(mainScreen);
		} else if (c == cmdCancel) {
			display.setCurrent(mainScreen);
		} else if (c == cmdEncodeText) {
			Alerter.alert(HexUtil
					.asHex(BinaryUtil.stringToUTFBytes(this.getString())));
		}
	}
}
